package com.qhit.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadFileServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		boolean isMultipart = ServletFileUpload.isMultipartContent(req);
		if (!isMultipart) {
			System.out.println(">> This wasn't a file upload request!");
			return;
		}

		PrintWriter out = resp.getWriter();

		// create factory and file cleanup tracker
		// FileCleaningTracker tracker = FileCleanerCleanup.getFileCleaningTracker(getServletContext());
		File tmpDir = new File(getBaseDir() + "/temp");
		DiskFileItemFactory factory = new DiskFileItemFactory(
				DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD, tmpDir);
		// factory.setFileCleaningTracker(tracker);

		// save upload file to disk
		ServletFileUpload upload = new ServletFileUpload(factory);
		try {
			List<FileItem> items = upload.parseRequest(req);
			String fileName = null;
			File savefile = null;
			for (FileItem item : items) {
				if (!item.isFormField()) {
					// 确定是文件而不是一个普通的表单字段
					fileName = item.getName();
					savefile = new File(getBaseDir() + "/" + fileName);
					item.write(savefile);
					System.out.println(">> [save] "
							+ savefile.getAbsolutePath());

					// to client info
					out.print("fileId=" + savefile.getAbsolutePath());
					out.flush();
				}
			}
		} catch (Exception e) {
			System.out.println(">> " + e.getMessage());
		}

	}

	private String getBaseDir() {
		return this.getServletContext().getRealPath("/upload");
	}

}
